package cn.iocoder.yudao.module.ym.controller.admin.tickettrajectory;

import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.ym.controller.admin.tickettrajectory.vo.*;
import cn.iocoder.yudao.module.ym.dal.dataobject.tickettrajectory.TicketTrajectoryDO;
import cn.iocoder.yudao.module.ym.service.tickettrajectory.TicketTrajectoryService;

@Tag(name = "管理后台 - 工单轨迹")
@RestController
@RequestMapping("/ym/ticket-trajectory")
@Validated
public class TicketTrajectoryController {

    @Resource
    private TicketTrajectoryService ticketTrajectoryService;

    @PostMapping("/create")
    @Operation(summary = "创建工单轨迹")
    public CommonResult<Long> createTicketTrajectory(@Valid @RequestBody TicketTrajectorySaveReqVO createReqVO) {
        return success(ticketTrajectoryService.createTicketTrajectory(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新工单轨迹")
    public CommonResult<Boolean> updateTicketTrajectory(@Valid @RequestBody TicketTrajectorySaveReqVO updateReqVO) {
        ticketTrajectoryService.updateTicketTrajectory(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除工单轨迹")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteTicketTrajectory(@RequestParam("id") Long id) {
        ticketTrajectoryService.deleteTicketTrajectory(id);
        return success(true);
    }

    @GetMapping("/getDay")
    @Operation(summary = "获得某个人当天工单轨迹")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<Map<String,Object>> getDay(@RequestParam("userId") Long userId,
                                                   @RequestParam("date") String date) {
        return success(ticketTrajectoryService.getDayByUserId(userId,date));
    }

    @GetMapping("/get")
    @Operation(summary = "获得工单轨迹")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<TicketTrajectoryRespVO> getTicketTrajectory(@RequestParam("id") Long id) {
        TicketTrajectoryDO ticketTrajectory = ticketTrajectoryService.getTicketTrajectory(id);
        return success(BeanUtils.toBean(ticketTrajectory, TicketTrajectoryRespVO.class));
    }

    @GetMapping("/list")
    @Operation(summary = "获得工单轨迹不分页")
    public CommonResult<List<TicketTrajectoryRespVO>> getTicketTrajectoryList(@Valid TicketTrajectoryPageReqVO pageReqVO) {
        List<TicketTrajectoryDO> pageResult = ticketTrajectoryService.getTicketTrajectoryList(pageReqVO);
        return success(BeanUtils.toBean(pageResult, TicketTrajectoryRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得工单轨迹分页")
    public CommonResult<PageResult<TicketTrajectoryRespVO>> getTicketTrajectoryPage(@Valid TicketTrajectoryPageReqVO pageReqVO) {
        PageResult<TicketTrajectoryDO> pageResult = ticketTrajectoryService.getTicketTrajectoryPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, TicketTrajectoryRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出工单轨迹 Excel")
    @ApiAccessLog(operateType = EXPORT)
    public void exportTicketTrajectoryExcel(@Valid TicketTrajectoryPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<TicketTrajectoryDO> list = ticketTrajectoryService.getTicketTrajectoryPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "工单轨迹.xls", "数据", TicketTrajectoryRespVO.class,
                        BeanUtils.toBean(list, TicketTrajectoryRespVO.class));
    }

}